/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dljs.classes;

import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Ariman
 */
public class GamePanel extends JPanel {
    
    public GamePanel (LayoutManager layout){
        super (layout);
        initData();
        this.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getButton() != MouseEvent.BUTTON1) return;
                int mouseX = evt.getPoint().x;
                int mouseY = evt.getPoint().y;
                System.out.println("click x="+mouseX+" y= "+mouseY);
                System.out.println("need for 1 and 2 y from "+GamePanel.getPanelY(1)+" to "+(GamePanel.getPanelY(1)+CardRenderer.CARD_HEIGHT));
                System.out.println("need for 3 y from "+GamePanel.getPanelY(3)+" to "+(GamePanel.getPanelY(3)+CardRenderer.CARD_HEIGHT+GamePanel.y_offset*20));
                if((mouseY>GamePanel.getPanelY(1))&&(mouseY<(GamePanel.getPanelY(1)+CardRenderer.CARD_HEIGHT))){
                    // up panels
                    if(mouseX>GamePanel.getPanelX(1, 0)&&(mouseX<(GamePanel.getPanelX(1,0)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field1Clicked(0);
                    }
                    if(mouseX>GamePanel.getPanelX(1, 1)&&(mouseX<(GamePanel.getPanelX(1,1)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field1Clicked(1);
                    }
                    if(mouseX>GamePanel.getPanelX(1, 2)&&(mouseX<(GamePanel.getPanelX(1,2)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field1Clicked(2);
                    }
                    if(mouseX>GamePanel.getPanelX(1, 3)&&(mouseX<(GamePanel.getPanelX(1,3)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field1Clicked(3);
                    }
                    
                    if(mouseX>GamePanel.getPanelX(2, 0)&&(mouseX<(GamePanel.getPanelX(2,0)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field2Clicked(0);
                    }
                    if(mouseX>GamePanel.getPanelX(2, 1)&&(mouseX<(GamePanel.getPanelX(2,1)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field2Clicked(1);
                    }
                    if(mouseX>GamePanel.getPanelX(2, 2)&&(mouseX<(GamePanel.getPanelX(2,2)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field2Clicked(2);
                    }
                    if(mouseX>GamePanel.getPanelX(2, 3)&&(mouseX<(GamePanel.getPanelX(2,3)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field2Clicked(3);
                    }
                    
                }else if((mouseY>GamePanel.getPanelY(3))&&(mouseY<(GamePanel.getPanelY(3)+CardRenderer.CARD_HEIGHT+GamePanel.y_offset*20))){
                    //down panels
                    if(mouseX>GamePanel.getPanelX(3, 0)&&(mouseX<(GamePanel.getPanelX(3,0)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(0);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 1)&&(mouseX<(GamePanel.getPanelX(3,1)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(1);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 2)&&(mouseX<(GamePanel.getPanelX(3,2)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(2);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 3)&&(mouseX<(GamePanel.getPanelX(3,3)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(3);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 4)&&(mouseX<(GamePanel.getPanelX(3,4)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(4);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 5)&&(mouseX<(GamePanel.getPanelX(3,5)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(5);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 6)&&(mouseX<(GamePanel.getPanelX(3,6)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(6);
                    }
                    if(mouseX>GamePanel.getPanelX(3, 7)&&(mouseX<(GamePanel.getPanelX(3,7)+CardRenderer.CARD_WIDTH))){
                        solitareEngine.Field3Clicked(7);
                    }
                }
                m_thisPanel.repaint();
            }
        });
    }
    
    public GamePanel() {
        initData();
    }
    
    @Override
    public void paintComponent(Graphics g) {
        m_backRenderer.renderBackground(getWidth(), getHeight(), g);        
        
        m_cardRenderer.renderCard(GamePanel.getPanelX(1, 0), GamePanel.getPanelY(1), g, solitareEngine.getCardSet().m_field1[0]);
        m_cardRenderer.renderCard(GamePanel.getPanelX(1, 1), GamePanel.getPanelY(1), g, solitareEngine.getCardSet().m_field1[1]);
        m_cardRenderer.renderCard(GamePanel.getPanelX(1, 2), GamePanel.getPanelY(1), g, solitareEngine.getCardSet().m_field1[2]);
        m_cardRenderer.renderCard(GamePanel.getPanelX(1, 3), GamePanel.getPanelY(1), g, solitareEngine.getCardSet().m_field1[3]);

        if (!solitareEngine.getCardSet().m_field2[0].isEmpty())
            m_cardRenderer.renderCard(GamePanel.getPanelX(2, 0), GamePanel.getPanelY(2), g, solitareEngine.getCardSet().m_field2[0].get(solitareEngine.getCardSet().m_field2[0].size()-1));
        if (!solitareEngine.getCardSet().m_field2[1].isEmpty())
            m_cardRenderer.renderCard(GamePanel.getPanelX(2, 1), GamePanel.getPanelY(2), g, solitareEngine.getCardSet().m_field2[1].get(solitareEngine.getCardSet().m_field2[1].size()-1));
        if (!solitareEngine.getCardSet().m_field2[2].isEmpty())
            m_cardRenderer.renderCard(GamePanel.getPanelX(2, 2), GamePanel.getPanelY(2), g, solitareEngine.getCardSet().m_field2[2].get(solitareEngine.getCardSet().m_field2[2].size()-1));
        if (!solitareEngine.getCardSet().m_field2[3].isEmpty())
            m_cardRenderer.renderCard(GamePanel.getPanelX(2, 3), GamePanel.getPanelY(2), g, solitareEngine.getCardSet().m_field2[3].get(solitareEngine.getCardSet().m_field2[3].size()-1));
        
        int x=base_x+x_offset*2;
        for (int i=0; i< 8; i++) {
            int y = GamePanel.getPanelY(3);
            for (CCard aCard : solitareEngine.getCardSet().m_field3[i]) {
                m_cardRenderer.renderCard(x, y, g, aCard);
                y += y_offset;
            }
            x += x_offset+CardRenderer.CARD_WIDTH;
        }
    }
    
    private void initData(){
        m_thisPanel = this;
        solitareEngine=new SolitareEngine();
        try {
            m_cardRenderer = new CardRenderer("cardSet");
            m_backRenderer = new BackgroundRenderer();
            
        } catch (IOException ex) {
            Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
    
    public static int getPanelX(int num,int position){
        int retX=base_x;
        switch(num){
            case 1: retX = base_x+x_offset*position+CardRenderer.CARD_WIDTH*position; 
                break;
            case 2: retX = base_x+x_offset*(position+4)+CardRenderer.CARD_WIDTH*(position+4)+x_offset*4;
                break;
            case 3: retX = base_x+x_offset*2+x_offset*position+CardRenderer.CARD_WIDTH*position;
                break;
        }
        return retX;
    };  
    
    public static int getPanelY(int num){
        int retY=base_y;
        switch(num){
            case 1: retY = base_y; 
                break;
            case 2: retY = base_y;
                break;
            case 3: retY = base_y+y_offset+CardRenderer.CARD_HEIGHT;
                break;
        }
        return retY;
    }; 
    public static final int base_x = 15;
    public static final int base_y = 15;
    public static final int x_offset = 10;
    public static final int y_offset = 30;
    // отрисовщик фона
    private BackgroundRenderer m_backRenderer = null;
    // отрисовщик карт
    private CardRenderer m_cardRenderer = null;
    private SolitareEngine solitareEngine;
    private GamePanel m_thisPanel;
}